package com.dhy.dbCore;

import com.dhy.dbCore.dataSource.MyDataSource;
import com.dhy.dbCore.transaction.core.JdbcTransactionTemplate;
import lombok.extern.slf4j.Slf4j;
import java.util.List;

/**
 * @author 大忽悠
 * @create 2022/6/7 10:34
 */
@Slf4j
public class JdbcTemplateFacade {
    private JdbcTemplate jt;
    private JdbcTransactionTemplate jtm;

    public JdbcTemplateFacade(MyDataSource myDataSource) {
        jt=new JdbcTemplate(myDataSource);
        jtm=new JdbcTransactionTemplate(myDataSource);
    }

    public Integer update(String sql){
        Integer res=null;
        try {
            res=jtm.execute(ts->{return jt.update(sql);});
        } catch (Throwable e) {
           log.error("update error :" ,e);
        }
        return res;
    }

    public Boolean insert(String sql) {
        Boolean res=null;
        try {
            res=jtm.execute(ts->{return jt.insert(sql);});
        } catch (Throwable e) {
            log.error("update error :" ,e);
        }
        return res;
    }

    public Boolean delete(String sql){
        Boolean res=null;
        try {
            res=jtm.execute(ts->{return jt.delete(sql);});
        } catch (Throwable e) {
            log.error("delete error :" ,e);
        }
        return res;
    }


    public <T> List<T> list(String sql, Class<T> targetClass){
        List<T> res=null;
        try {
            res=jtm.execute(ts->{return jt.list(sql,targetClass);});
        } catch (Throwable e) {
            log.error("list error :" ,e);
        }
        return res;
    }

    public <T> T queryOne(String sql,Class<T> targetClass){
        T res=null;
        try {
            res=jtm.execute(ts->{return jt.queryOne(sql,targetClass);});
        } catch (Throwable e) {
            log.error("queryOne error :" ,e);
        }
        return res;
    }

    public Boolean execute(String sql){
        Boolean res=null;
        try {
            res=jtm.execute(ts->{return jt.delete(sql);});
        } catch (Throwable e) {
            log.error("execute error :" ,e);
        }
        return res;
    }
}
